﻿package com.eshore.player;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;

import com.eshore.R;

public class ShowRtmpPlayer extends Activity implements OnTouchListener {

	public String rtmpurl = "";
	Intent intent;
	Thread ptzThreader = null;

	ControlPanelView cpview = null;
	boolean showPtzButton = false;
	boolean showControlPanelButton = false;
	public static ShowRtmpPlayer _this_=null;
	@Override
	protected void onDestroy() {
		_this_ = null;
		super.onDestroy();
	}

	private int status = ShowRtmpPlayer.PTZSTATUS_INDE;

	public static int PTZSTATUS_INDE = 0;
	public static int PTZSTATUS_DOING = 1;
	public static int PTZSTATUS_SUCCESS = 2;
	public static int PTZSTATUS_FAILE = 3;
	public static int PTZSTATUS_MALFORMEDURLEXCEPTION = 10;
	public static int PTZSTATUS_IOEXCEPTION = 10;
	public static int PTZSTATUS_PARSERCONFIGURATIONEXCEPTION = 10;
	public static int PTZSTATUS_SAXEXCEPTION = 10;

	public static ImagePlayerViewRtmp playerView = null;
	private PowerManager.WakeLock wakeLock = null;

	private Thread timerThread = null;
	private boolean timerTouch = false; 
	public static int screenWidth = 0;
	public static int screenHeight = 0;
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		Log.d("Test","OK1");
		
		_this_ = this;

		ImagePlayerView.initLib();
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.empty_horizontal); // 设置用户界面
		Log.d("Test","OK2");
		intent = ShowRtmpPlayer.this.getIntent();
		// intent.getDataString();
		setTitle("测试视频"); // 设置title

		
		{ // get display size
			// WindowManager windowManager = getWindowManager();
			// Display display = windowManager.getDefaultDisplay();
			// screenWidth = display.getWidth();
			// screenHeight = display.getHeight();

			// 取得屏幕信息
			DisplayMetrics dm = new DisplayMetrics();
			dm = this.getResources().getDisplayMetrics();
			// 获得屏幕宽度
			screenWidth = dm.widthPixels;
			// 获得屏幕高度
			screenHeight = dm.heightPixels;
		}
		Log.d("Test","OK3");
		// playerView = new ImagePlayerView(this,
		// ImagePlayerView.MODE_FULLSCREEN, screenWidth, screenHeight, rtmpurl);
		// rtmpurl = this.getString(Resource.string.streamURL);
		rtmpurl = this.getIntent().getStringExtra("filename");
		playerView = new ImagePlayerViewRtmp(this, ImagePlayerView.MODE_FULLSCREEN,
				screenWidth, screenHeight, rtmpurl);
		this.setContentView(playerView);
		Log.d("Test","OK4");
		playerView.start();
		this.cpview = new ControlPanelView(this, screenWidth, screenHeight, 1);
		RelativeLayout.LayoutParams parama = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); // LayoutParams.WRAP_CONTENT
		parama.setMargins(0, 0, parama.rightMargin, parama.bottomMargin);
		this.addContentView(cpview, parama);
		this.cpview.setAllOnTouchListener(this);
		this.showPtzButton = false;
		this.showControlPanelButton = false;
		PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,
				"player");
		wakeLock.acquire();

		timerThread = new TimerThread();
	}
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onResume() {
		this.resumeValue();
		super.onResume();
	}

	@Override
	protected void onStart() {

		Message message = new Message();
		message.what = 1;
		handler.sendMessage(message);

		super.onStart();
	}

	@Override
	protected void onPause() {
		this.pauseValue();
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	Handler handler = new Handler() {

		public void handleMessage(Message msg) {

			switch (msg.what) {
			case 1:
				cpview.setVisible(showControlPanelButton);
				// checkPtzDisplay();
				break;
			case 2:
				try {
					if (ptzThreader != null) {
						Thread.sleep(50);
						ptzThreader.start();
						Thread.sleep(50);
					}
				} catch (Exception e) {

				}
				break;
			case 3:
				try {
					if (ptzThreader != null) {
						Thread.sleep(50);
						ptzThreader.stop();
						Thread.sleep(50);
					}
				} catch (Exception e) {

				}
				break;
			case 4:
				cpview.setVisible(true);
//				cpview.setPtzVisible(showPtzButton);
				break;
			case 5:

//				cpview.visibleParam();
//				cpview.disvisibleParam();
//
				cpview.setVisible(false);
//				cpview.setPtzVisible(showPtzButton);
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	private void stopValue() {
		playerView.stop();
	}

	public void pauseValue() {
		playerView.pause();
	}

	public void resumeValue() {
		playerView.resume();
	}
	
	// 计时 20 \ 10 隐藏CONTROLPANEL
	class TimerThread extends Thread {

		public TimerThread() {
			
		}
		@Override
		public void run() {
			try {
				sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return;
			}
			showPtzButton = true;
			showControlPanelButton = true;
			Message message = null;
			message = new Message();
			message.what = 5;
			handler.sendMessage(message);
			int timer = 0;
			while (true) {
				try {
					timerTouch = false;
					sleep(1000); // 1秒
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return;
				}
				if (showPtzButton) {
					if(timerTouch) {
						timer = 0;
					} else {
						timer++;
						if(timer > 20) {
							showPtzButton = false;
							showControlPanelButton = false;
							message = new Message();
							message.what = 4;
							handler.sendMessage(message);
						}
					}
				} else {
					if(timerTouch) {
						timer = 0;
					} else {
						timer++;
						if(timer > 10) {
							showPtzButton = false;
							showControlPanelButton = false;
							message = new Message();
							message.what = 4;
							handler.sendMessage(message);
						}
					}
				}
			}
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		timerTouch = true;
		if ((v.getId() == R.id.controlface)
				&& (event.getAction() == MotionEvent.ACTION_DOWN)) {
			this.showControlPanelButton = !this.showControlPanelButton;
			this.cpview.setVisible(showControlPanelButton);
		}
		return false;
	}
	
}
